#include "head.h"

#define FIFO_NAME "./fifo_test"

void FunReadStdin( int SignalNum )
{
    char strBuff[1024] = {0};
    read( 0, strBuff, sizeof(strBuff) );
    printf( "Recv from stdin: %s", strBuff );
}

int main(int argc, char const *argv[])
{
    signal( SIGIO, FunReadStdin );

    mkfifo( FIFO_NAME, 0664 );

    int FifoFd;
    FifoFd = open( FIFO_NAME, O_RDONLY );
    if ( -1 == FifoFd )
    {
        perror( "fail open fifo" );
        return -1;
    }

    // 1. 设置标准输入设备 (0) 的属性
    int Flag;
    Flag = fcntl( 0, F_GETFL );
    Flag |= O_ASYNC;
    fcntl( 0, F_SETFL, Flag );

    // 2. 关联本进程( 指定异步IO通知的进程为本进程 )
    fcntl( 0, F_SETOWN, getpid() );

    int i;
    for( i = 1; i <= 110; ++i )
    {
        char strBuff[1024] = {0};
        read( FifoFd, strBuff, sizeof(strBuff) );
        printf( "Counter %3d --- Recv from FIFO: %s\n", i, strBuff );
    }

    close( FifoFd );

    return 0;
}
